package com.ccut.xunfeiSpringboot.service.impl;/*
 * ClassName: SchoolServiceImpl
 * Package: com.ccut.xunfeiSpringboot.service.impl
 * Description:
 * @Author 刘千湫
 * @Create 2025/5/27 21:19
 * @Version 1.0
 */



import com.ccut.xunfeiSpringboot.entity.School;
import com.ccut.xunfeiSpringboot.entity.Student;
import com.ccut.xunfeiSpringboot.exception.CustomException;
import com.ccut.xunfeiSpringboot.mapper.SchoolMapper;
import com.ccut.xunfeiSpringboot.service.SchoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SchoolServiceImpl implements SchoolService {

    @Resource
    private SchoolMapper schoolMapper;

    @Override
    public void add(School school) {
        String schoolName = school.getSchoolName();
        School dbSchool = schoolMapper.selectBySchoolName(schoolName);
        if (dbSchool != null) {
            throw new CustomException("500", "学校名称已存在，请更换别的名称");
        }
        // 设置默认值逻辑（可选）
        if (school.getCreatedAt() == null) {
            school.setCreatedAt(new java.sql.Timestamp(System.currentTimeMillis()));
        }
        if (school.getUpdatedAt() == null) {
            school.setUpdatedAt(new java.sql.Timestamp(System.currentTimeMillis()));
        }

        schoolMapper.insertSchool(school);
    }

    @Override
    public void deleteById(Integer schoolId) {
        schoolMapper.deleteById(schoolId);
    }

    @Override
    public void update(School school) {
        schoolMapper.updateById(school);
    }

    @Override
    public List<School> selectAll() {
        return schoolMapper.selectAll();
    }

    @Override
    public School selectById(Integer schoolId) {
        return schoolMapper.selectById(schoolId);
    }

    @Override
    public PageInfo<School> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<School> list = schoolMapper.selectAll();
        return PageInfo.of(list);
    }

    @Override
    public boolean existsBySchoolName(String schoolName) {
        return schoolMapper.selectBySchoolName(schoolName) != null;
    }

    @Override
    public School login(String username) {
        School school = schoolMapper.selectByUsername(username);
//        if (student == null) {
//            throw new CustomException("500", "账号不存在");
//        }
        return school;
    }

    @Override
    public boolean existsByUsername(String username) {
        return false;
    }

    @Transactional
    @Override
    public void register(School school) {
        this.add(school);
    }
}
